import ScrollBarLayout from '@/components/ScrollBarLayout';
import Cls from 'classnames';
import pageStyles from '@/common/pageLayout.less';
import { useEffect, useMemo, useState } from 'react';
import { connect } from 'umi';
import { Col, Form, Row, Spin, message, Tabs, Table } from 'antd';
import { MyAssetListManageAPI } from '../common/type';
import { getLifeCycleService, getChangeLogService } from '../services';
import { commonPageSizeOptions, showTotalInfo } from '@/common/constVariable';
import { EditAssetLogView } from './EditAssetLogView';
import {
  getApplyTableColumns,
  getTransferTableColumns,
  getOutStoreTableColumns,
  getScrapTableColumns,
  getMaintenanceTableColumns,
  getChangeLogTableColumns,
} from '../mconfig/NodeTablecolumn';

const LifeCycleView: React.FC<any> = ({ curRes, id }) => {
  /** 样式根目录 */
  const styleFixCls = 'contract-styfixcls-';

  /** 表格数据 */
  const [dataSource, setDataSource] = useState<Record<string, any>[]>([]);

  /** 当前行数据 */
  const [rowData, setRowData] = useState<Record<string, any>>({});

  /** 资产修改历史弹窗 */
  const [editAssetLogModalStatus, setEditAssetLogModalStatus] = useState<boolean>(false);

  /** 打开资产修改历史弹窗 */
  const openEditModal = (record: Record<string, any>) => {
    let obj: any = JSON.parse(JSON.stringify(record));
    obj.beforeJson = JSON.parse(obj.beforeJson);
    obj.operParam = JSON.parse(obj.operParam);
    setRowData(obj);
    setEditAssetLogModalStatus(true);
  };

  /** 获取生命周期回调 */
  const getLifeCycleCallback = (res: any, clickType: boolean) => {
    if (res && res?.msgFlag) {
      message.success(res?.msg);
      setDataSource(res.obj);
    } else {
      message.error(res?.msg);
    }
  };

  /** 获取修改日志回调 */
  const getChangeLogCallback = (res: any, clickType: boolean) => {
    if (res && res?.msgFlag) {
      message.success(res?.msg);
      setDataSource(res.obj);
    } else {
      message.error(res?.msg);
    }
  };

  /** 获取生命周期 */
  const { getLifeCyclelRun, getLifeCyclelLoading } = getLifeCycleService(getLifeCycleCallback);

  /** 获取修改日志 */
  const { getChangeLogListTableData, changeLogData, loading, pagination, otherParams } = getChangeLogService(getChangeLogCallback);

  /** 重置方法 */
  const onReset = (otherParams: MyAssetListManageAPI.IAssetTableRequestType) => {
    getChangeLogListTableData({ ...otherParams });
  };

  /** 表格刷新数据 */
  const reloadTableData = () => {
    getChangeLogListTableData({});
  };

  /** 分页方法 */
  const onPageChange = (current: any, pageSize: any) => {
    getChangeLogListTableData({ pageSize, current });
  };

  /** 获取当前表格数据 */
  const getTableData = (value: string) => {
    getLifeCyclelRun({
      id,
      applyType: value,
    });
  };

  /**  切换表格 */
  const changeTable = (activeKey: string) => {
    /**  领用 */
    if (activeKey == 'use') getTableData('ASSET_APPLY_UES');
    /**  交接 */
    if (activeKey == 'transfer') getTableData('ASSET_APPLY_ALLOT');
    /**  退库 */
    if (activeKey == 'return') getTableData('ASSET_APPLY_RETURN');
    /**  报废 */
    if (activeKey == 'dispose') getTableData('ASSET_APPLY_HANDLE');
    /**  维保 */
    if (activeKey == 'maintenance') getTableData('ASSET_MAINTEN');
    /**  修改日志 */
    if (activeKey == 'changeLog') getChangeLogListTableData({ assetInfoId: id });
  };

  /**  获取表格配置 */
  const ApplyTableColumns: Record<string, any>[] = useMemo(() => {
    return getApplyTableColumns();
  }, [curRes]);
  const TransferTableColumns: Record<string, any>[] = useMemo(() => {
    return getTransferTableColumns();
  }, [curRes]);
  const OutStoreTableColumns: Record<string, any>[] = useMemo(() => {
    return getOutStoreTableColumns();
  }, [curRes]);
  const ScrapTableColumns: Record<string, any>[] = useMemo(() => {
    return getScrapTableColumns();
  }, [curRes]);
  const MaintenanceTableColumns: Record<string, any>[] = useMemo(() => {
    return getMaintenanceTableColumns();
  }, [curRes]);
  const ChangeLogTableColumns: Record<string, any>[] = useMemo(() => {
    return getChangeLogTableColumns(openEditModal);
  }, [curRes]);

  useEffect(() => {
    changeTable('use');
  }, []);

  return (
    <>
      <Spin spinning={false}>
        <Row gutter={[8, 8]} className={`${styleFixCls}row`}>
          <Col span={24} className={`${styleFixCls}row-col`}>
            <Tabs
              className={pageStyles['commonTabs']}
              type="card"
              onChange={(activeKey: string) => {
                changeTable(activeKey);
              }}
              items={[
                {
                  label: '资产领用',
                  key: 'use',
                  children: (
                    <>
                      <div className="commonTabsBodyDom">
                        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
                          <ScrollBarLayout>
                            <div className={Cls('list-table', `${styleFixCls}updateStepsBox-tableBox`)}>
                              <Table
                                size="small"
                                bordered
                                scroll={{ x: 1200, y: 'true' }}
                                columns={ApplyTableColumns}
                                loading={getLifeCyclelLoading}
                                dataSource={dataSource}
                                rowKey={(row) => row?.id}
                                // pagination={{
                                //     ...pagination,
                                //     showQuickJumper: true,
                                //     showSizeChanger: true,
                                //     pageSizeOptions: commonPageSizeOptions,
                                //     showTotal: tableTotalShow ? tableTotalShow : showTotalInfo,
                                //     onChange: onPageChange,
                                //     // pageSize: pagination?.pageSize,
                                //     // current: pagination?.current,
                                // }}
                              />
                            </div>
                          </ScrollBarLayout>
                        </div>
                      </div>
                    </>
                  ),
                },
                {
                  label: '资产交接',
                  key: 'transfer',
                  children: (
                    <>
                      <div className="commonTabsBodyDom">
                        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
                          <ScrollBarLayout>
                            <div className={Cls('list-table', `${styleFixCls}updateStepsBox-tableBox`)}>
                              <Table
                                size="small"
                                bordered
                                scroll={{ x: 1200, y: 'true' }}
                                columns={TransferTableColumns}
                                loading={getLifeCyclelLoading}
                                dataSource={dataSource}
                                rowKey={(row) => row?.id}
                                // pagination={{
                                //     ...pagination,
                                //     showQuickJumper: true,
                                //     showSizeChanger: true,
                                //     pageSizeOptions: commonPageSizeOptions,
                                //     showTotal: tableTotalShow ? tableTotalShow : showTotalInfo,
                                //     onChange: onPageChange,
                                //     // pageSize: pagination?.pageSize,
                                //     // current: pagination?.current,
                                // }}
                              />
                            </div>
                          </ScrollBarLayout>
                        </div>
                      </div>
                    </>
                  ),
                },
                {
                  label: '资产退库',
                  key: 'return',
                  children: (
                    <>
                      <div className="commonTabsBodyDom">
                        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
                          <ScrollBarLayout>
                            <div className={Cls('list-table', `${styleFixCls}updateStepsBox-tableBox`)}>
                              <Table
                                size="small"
                                bordered
                                scroll={{ x: 1200, y: 'true' }}
                                columns={OutStoreTableColumns}
                                loading={getLifeCyclelLoading}
                                dataSource={dataSource}
                                rowKey={(row) => row?.id}
                                // pagination={{
                                //     ...pagination,
                                //     showQuickJumper: true,
                                //     showSizeChanger: true,
                                //     pageSizeOptions: commonPageSizeOptions,
                                //     showTotal: tableTotalShow ? tableTotalShow : showTotalInfo,
                                //     onChange: onPageChange,
                                //     // pageSize: pagination?.pageSize,
                                //     // current: pagination?.current,
                                // }}
                              />
                            </div>
                          </ScrollBarLayout>
                        </div>
                      </div>
                    </>
                  ),
                },
                {
                  label: '资产维保',
                  key: 'maintenance',
                  children: (
                    <>
                      <div className="commonTabsBodyDom">
                        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
                          <ScrollBarLayout>
                            <div className={Cls('list-table', `${styleFixCls}updateStepsBox-tableBox`)}>
                              <Table
                                size="small"
                                bordered
                                scroll={{ x: 1200, y: 'true' }}
                                columns={MaintenanceTableColumns}
                                loading={getLifeCyclelLoading}
                                dataSource={dataSource}
                                rowKey={(row) => row?.id}
                              />
                            </div>
                          </ScrollBarLayout>
                        </div>
                      </div>
                    </>
                  ),
                },
                {
                  label: '资产报废',
                  key: 'dispose',
                  children: (
                    <>
                      <div className="commonTabsBodyDom">
                        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
                          <ScrollBarLayout>
                            <div className={Cls('list-table', `${styleFixCls}updateStepsBox-tableBox`)}>
                              <Table
                                size="small"
                                bordered
                                scroll={{ x: 1200, y: 'true' }}
                                columns={ScrapTableColumns}
                                loading={getLifeCyclelLoading}
                                dataSource={dataSource}
                                rowKey={(row) => row?.id}
                                // pagination={{
                                //     ...pagination,
                                //     showQuickJumper: true,
                                //     showSizeChanger: true,
                                //     pageSizeOptions: commonPageSizeOptions,
                                //     showTotal: tableTotalShow ? tableTotalShow : showTotalInfo,
                                //     onChange: onPageChange,
                                //     // pageSize: pagination?.pageSize,
                                //     // current: pagination?.current,
                                // }}
                              />
                            </div>
                          </ScrollBarLayout>
                        </div>
                      </div>
                    </>
                  ),
                },
                {
                  label: '资产变更记录',
                  key: 'changeLog',
                  children: (
                    <>
                      <div className="commonTabsBodyDom">
                        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
                          <ScrollBarLayout>
                            <div className={Cls('list-table', `${styleFixCls}updateStepsBox-tableBox`)}>
                              <Table
                                size="small"
                                bordered
                                scroll={{ x: 1200, y: 'true' }}
                                columns={ChangeLogTableColumns}
                                loading={loading}
                                dataSource={changeLogData?.rows ? changeLogData?.rows : []}
                                rowKey={(row) => row?.id}
                                pagination={{
                                  ...pagination,
                                  showQuickJumper: true,
                                  showSizeChanger: true,
                                  pageSizeOptions: commonPageSizeOptions,
                                  showTotal: showTotalInfo,
                                  onChange: onPageChange,
                                }}
                              />
                            </div>
                          </ScrollBarLayout>
                        </div>
                      </div>
                    </>
                  ),
                },
              ]}
              destroyInactiveTabPane={true}
            />

            {/* 资产修改历史表单弹窗 */}
            {editAssetLogModalStatus ? (
              <EditAssetLogView
                rowData={rowData}
                curRes={curRes}
                onClose={() => {
                  setEditAssetLogModalStatus(false);
                }}
                type={'detail'}
              ></EditAssetLogView>
            ) : null}
          </Col>
        </Row>
      </Spin>
    </>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(LifeCycleView);
